<template>
  <div class="checkout-address">
    <div class="text">
      <ul v-if="arr2">
        <li>
          <span>收<i />货<i />人：</span>{{arr2.receiver}}
        </li>
        <li><span>联系方式：</span>{{filterNum(arr2.contact)}}</li>
        <li><span>收货地址：</span>{{arr2.fullLocation.replace(/ /g, "") + arr2.address}}</li>
      </ul>
      <div class="none" v-else>您需要先添加收货地址才可提交订单。</div>
      <a href="javascript:;" @click="dingCom.open(arr2)">修改地址</a>
      <a href="javascript:;" @click="delSite(arr2.id)">删除地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="isShow = true">切换地址</XtxButton>
      <XtxButton class="btn" @click="dingCom.open()">添加地址</XtxButton>
    </div>
    <!-- 切换地址弹出层 -->
    <XtxDialog title="切换地址" v-model="isShow">
      <div
        class="text item"
        v-for="item in list"
        :key="item.id"
        @click="selectSite(item)"
        :class="{ active: temp === item.id }"
      >
        <ul>
          <li>
            <span>收<i />货<i />人：</span>{{ item.receiver }}
          </li>
          <li><span>联系方式：</span>{{ filterNum(item.contact) }}</li>
          <li>
            <span>收货地址：</span
            >{{ item.fullLocation.replace(/ /g, "") + item.address }}
          </li>
        </ul>
      </div>
      <template #footer>
        <XtxButton type="gray" style="margin-right: 20px" @click="isShow=false">取消</XtxButton>
        <XtxButton type="primary" @click="OkBtn">确认</XtxButton>
      </template>
    </XtxDialog>
    <!-- 添加地址弹出层 -->
    <AddressEdit @upSite='upSite' ref="dingCom" />
  </div>
</template>

<script>
import { ref } from 'vue'
import Message from '@/components/library/Message'
import AddressEdit from './address-edit.vue'
import { deleteAddress } from '@/api/order'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup (props, { emit }) {
    // 电话号码的处理函数
    const filterNum = (num) => {
      return num.slice(0, 3) + '****' + num.slice(-4)
    }
    // 对话框的显示隐藏
    const isShow = ref(false)
    const temp = ref(null)
    const arr = ref(null)
    // 选择地址
    const selectSite = (item) => {
      temp.value = item.id
      arr.value = item
    }
    const arr2 = ref(props.list[0])
    // 默认渲染一个地址
    emit('change1', arr2.value && arr2.value.id)
    // 点击确定按钮
    const OkBtn = () => {
      if (!arr.value) return Message({ type: 'warn', text: '没有选择地址' })
      arr2.value = arr.value
      isShow.value = false
      // 把地址ID传给父组件，支付接口需要
      emit('change1', arr2.value.id)
    }
    // 组件的ref标识
    const dingCom = ref(null)
    // 点击删除地址
    const delSite = async (id) => {
      await deleteAddress(id)
      arr2.value = props.list.filter(v => v.id !== id)[0]
    }
    // 接收子组件传递的信息，用来更新渲染页面
    const upSite = (val) => {
      arr2.value = { ...val }
    }
    return { isShow, temp, selectSite, OkBtn, arr2, filterNum, dingCom, delSite, upSite }
  }
}
</script>

<style scoped lang="less">
.checkout-address {
  .xtx-dialog {
    .text {
      flex: 1;
      min-height: 90px;
      display: flex;
      align-items: center;
      &.item {
        border: 1px solid #f5f5f5;
        margin-bottom: 10px;
        cursor: pointer;
        &.active,
        &:hover {
          border-color: @xtxColor;
          background: lighten(@xtxColor, 50%);
        }
        > ul {
          padding: 10px;
          font-size: 14px;
          line-height: 30px;
        }
      }
    }
  }
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
